package com.koron.sinoprobe.web.engineering.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;

import java.util.List;
import java.util.Map;

/**
 * @author jzp
 * @since 2021/12/8
 */
public class SpDivisionProjectListener extends AnalysisEventListener<Map<Integer, String>> {

    private List<Map<Integer, String>> listMap;

    public SpDivisionProjectListener(List<Map<Integer, String>> listMap) {
        this.listMap = listMap;
    }

    @Override
    public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
        listMap.add(integerStringMap);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        int firstRowIndex = extra.getFirstRowIndex() - 1;
        int firstColumnIndex = extra.getFirstColumnIndex();
        int lastRowIndex = extra.getLastRowIndex() - 1;
        int lastColumnIndex = extra.getLastColumnIndex();

        if (extra.getType().equals(CellExtraTypeEnum.MERGE)) {
            // 获取首个单元格的值
            String s = listMap.get(firstRowIndex).get(firstColumnIndex);
            // 填充给其他的单元格
            for (int i = firstRowIndex; i <= lastRowIndex; i++) {
                for (int j = firstColumnIndex; j <= lastColumnIndex; j++) {
                    listMap.get(i).put(j, s);
                }
            }
        }
    }
}
